package cn.com.hongfu.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.hongfu.model.InfoItem;
import cn.com.hongfu.model.InfoSet;
import cn.com.hongfu.service.InfoSetService;

@Controller
@RequestMapping("infoSet")
public class InfoSetController {
	@Autowired
	public InfoSetService infoSetService ;
	@RequestMapping("addUI")
	public String addUI(){
		return "/infoset/infoSetAdd";
	}
	@RequestMapping("add")
	public String add(HttpServletRequest request,ModelMap modeMap){
		String name = request.getParameter("iName");
		String desc = request.getParameter("iDesc");
		InfoSet infoSet = new InfoSet();
		infoSet.setiName(name);
		infoSet.setiDesc(desc);
		infoSetService.save(infoSet);
		return "redirect:/infoSet/list.html";
	}
	@RequestMapping("list")
	public String list(ModelMap modelMap){
		modelMap.addAttribute("infoSets", infoSetService.getAll());
		return "/infoset/infoSetList";
	}
	@RequestMapping("infoItemList")
	public String infoItemList(ModelMap modelMap,HttpServletRequest request){
		int infoSetId = Integer.parseInt(request.getParameter("infoSetId"));
		modelMap.addAttribute("infoSetId", infoSetId);
		modelMap.addAttribute("infoItems",infoSetService.getItemByInfoSetId(infoSetId));
		return "/infoset/infoItemList";
	}
	@RequestMapping("infoItemAdd")
	public String infoItemAdd(ModelMap modelMap,HttpServletRequest request){
		String iName = request.getParameter("iName");
		String iValue = request.getParameter("iValue");
		int infoSetId = Integer.parseInt(request.getParameter("infoSetId"));
		InfoItem infoItem = new InfoItem();
		infoItem.setiName(iName);
		infoItem.setiValue(iValue);
		infoItem.setInfoSetId(infoSetId);
		infoSetService.saveItem(infoItem);
		modelMap.addAttribute("infoSetId", infoSetId);
		return "redirect:/infoSet/list.html";
	}
}
